#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;



string boy[55];
string girl[55];
string stu[55];
int arr[55];
int main()
{
    int n, flap = 0, c1 = 0, c2 = 0;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        string tmp;
        cin >> flap >> tmp;
        stu[i] = tmp;
        if (flap)
        {
            arr[i] = 1;
            boy[c1++] = tmp;
        }
        else
        {
            girl[c2++] = tmp;
        }
    }
    int i = 0;
    while (stu[i] != girl[c2] && stu[i] != boy[c1])
    {
        cout << stu[i] << ' ';
        if (arr[i])
        {
            cout << girl[--c2];
        }
        else
        {
            cout << boy[--c1];
        }
        i++;
        cout << endl;
    }
    return 0;
}